package com.witsight.hibernate.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.engine.spi.NamedQueryDefinition;

import com.witsight.hibernate.dao.ICityRepository;
import com.witsight.hibernate.model.City;
import com.witsight.hibernate.util.SqlBuilder;
import com.witsight.platform.model.BoEntity;

/**   
 * 说明：[]
 * @Title: CityRepositoryImpl.java 
 * @Package com.witsight.hibernate.dao.impl 
 * @See: []
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: fuyp  
 * @date: 2017年9月18日 下午4:22:20 
 * @version: V1.0
 *
 */
public class CityRepositoryImpl extends BaseJpaRepositoryImpl implements ICityRepository {
	public City findCityTest() {
		System.out.println("111");
		NamedQueryDefinition namedQueryDefinition = getNamedSQLQuery("City.findSqlCityPage");
		BoEntity params = new BoEntity();
		String queryString = SqlBuilder.parse(namedQueryDefinition.getQueryString(), params);
	 	Query q= this.getEm().createQuery(queryString);
	 	List<String> list= q.getResultList();
	 	City city = new City();
	 	city.setName(list.size()>0?list.get(0):null);
	 	return city;
	}
	
	public City saveTest(){
		City c = new City();
		c.setCountry("11111111");
		c.setName("bbbbbbbbbbb");
	    this.getEm().persist(c);
		return c;
	}
	public void d1eleteTest(String id) {
		EntityManager em = this.getEm();
		City c = em.find(City.class, id);
	    em.remove(em.contains(c) ? c : em.merge(c));
	}
	public City u1pdateTest(){
		City c = getEm().find(City.class, "402881e65f043c6d015f043c78400000");
		c.setVersion(c.getVersion()+1);
		this.getEm().merge(c);
		return c;
	}
	public City updateTest(){
		City c = getEm().find(City.class, "402881e65f043c6d015f043c78400000");
		c.setVersion(c.getVersion()+1);
		this.getEm().merge(c);
		return c;
	}
}
